package com.rc.saas.tenant.service.ticket;

import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.mapper.ticket.TenantTicketScheduleMapper;
import com.rc.saas.tenant.model.member.MemberInfo;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.model.ticket.MemberTicket;
import com.rc.saas.tenant.model.ticket.TenantTicketSchedule;
import com.rc.saas.tenant.model.ticket.TenantTicketType;
import com.rc.saas.tenant.service.member.MemberInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author frog
 * @date 2021/2/25
 */
@Service
public class TenantTicketScheduleServiceImpl implements TenantTicketScheduleService {

    @Autowired
    private TenantTicketScheduleMapper tenantTicketScheduleMapper;

    @Autowired
    private TenantTicketTypeService tenantTicketTypeService;

    @Autowired
    private MemberTicketService memberTicketService;

    @Override
    public TenantTicketScheduleMapper getMapper() {
        return tenantTicketScheduleMapper;
    }

    @Override
    public TResponse saveTimeingGiveAway(TenantTicketSchedule tenantTicketSchedule, TenantUser user) {
        // send_type 赠送类型 0一次性赠送 1重复赠送 默认0
        tenantTicketSchedule.setSendType("1");
        //状态 0已启用 1已停用 默认0
        tenantTicketSchedule.setStatus("0");
        tenantTicketSchedule.setTenantCode(user.getTenantCode());
        tenantTicketSchedule.setRemainCount(tenantTicketSchedule.getTicketScheduleCount());

        //    String[] moblieList = tenantTicketSchedule.getMemberMobile().split(",");
        //    List<TenantTicketSchedule> memberTicketScheduleList = new ArrayList();
        //
        //    StringBuilder nonMembers = new StringBuilder();
        //    for (String s : moblieList) {
        //        MemberInfo memberInfo = memberInfoService.selectMemberByMobile(user.getTenantCode(), s);
        //        if (memberInfo != null) {
        //            tenantTicketSchedule.setMemberId(memberInfo.getMemberId());
        //            tenantTicketSchedule.setMemberMobile(s);
        //            memberTicketScheduleList.add(tenantTicketSchedule);
        //        } else {
        //            nonMembers.append(s).append(",");
        //        }
        //    }
        //   tenantTicketScheduleMapper.insertBatch(memberTicketScheduleList);
        return TResponse.SUCCESS();
    }

    @Override
    public void ticketTask() {
        Date date = new Date();
        MemberTicket memberTicket = new MemberTicket();
        memberTicket.setTicketSource("0");
        memberTicket.setStatus("0");
        memberTicket.setGainTime(date);
        memberTicket.setCreatedTime(new Date());

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int weekDay = calendar.get(Calendar.DAY_OF_WEEK);

        int monthDay = calendar.get(Calendar.DAY_OF_MONTH);
        // schedule_type 发送类型  0按天（立即） 1按周 2按月  默认0

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String today = format.format(date);

        //判断是否为当月一号(首次赠送不能为今天)
        if (monthDay == 1) {
            List<TenantTicketSchedule> tenantTicketSchedules = tenantTicketScheduleMapper.listByScheduleType("2", today);
            if (!CollectionUtils.isEmpty(tenantTicketSchedules)) {
                insertMemberTicket(memberTicket, tenantTicketSchedules);
            }
        }

        //判断是否为周一(首次赠送不能为今天)
        if (weekDay == 2) {
            List<TenantTicketSchedule> tenantTicketSchedules = tenantTicketScheduleMapper.listByScheduleType("1", today);
            if (!CollectionUtils.isEmpty(tenantTicketSchedules)) {
                insertMemberTicket(memberTicket, tenantTicketSchedules);
            }
        }

        //每天
        List<TenantTicketSchedule> tenantTicketSchedules = tenantTicketScheduleMapper.listByScheduleType("0", today);
        if (!CollectionUtils.isEmpty(tenantTicketSchedules)) {
            insertMemberTicket(memberTicket, tenantTicketSchedules);
        }


        List<TenantTicketSchedule> listByFirstSendTimeirstSendTime = tenantTicketScheduleMapper.listByFirstSendTime();
        if (!CollectionUtils.isEmpty(listByFirstSendTimeirstSendTime)) {
            insertMemberTicket(memberTicket, listByFirstSendTimeirstSendTime);
        }

    }

    private void insertMemberTicket(MemberTicket memberTicket, List<TenantTicketSchedule> tenantTicketSchedules) {
        for (TenantTicketSchedule tenantTicketSchedule : tenantTicketSchedules) {
            TenantTicketType tenantTicketType = tenantTicketTypeService.selectByPrimaryKey(tenantTicketSchedule.getTicketTypeId());
            memberTicket.setTenantCode(tenantTicketType.getTenantCode());
            memberTicket.setMemberId(tenantTicketSchedule.getMemberId());
            memberTicket.setTicketTypeId(tenantTicketSchedule.getTicketTypeId());
            Date date = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            if ("0".equals(tenantTicketType.getEffectType())) {
                calendar.add(Calendar.DATE, tenantTicketType.getEffectDay());
                memberTicket.setEndTime(calendar.getTime());
            } else if ("1".equals(tenantTicketType.getEffectType())) {
                memberTicket.setEndTime(tenantTicketType.getEffectEndDate());
            }
            memberTicket.setTicketSendRecordId(tenantTicketSchedule.getTicketSendRecordId());

            Integer periodCount = tenantTicketSchedule.getPeriodCount();
            for (int i = 0; i < periodCount; i++) {
                memberTicketService.insertSelective(memberTicket);
            }

            tenantTicketSchedule.setRemainCount(tenantTicketSchedule.getRemainCount() - 1);
            tenantTicketScheduleMapper.updateByPrimaryKeySelective(tenantTicketSchedule);
        }
    }
}
